<?php namespace Admin\Controller;
class TypeController extends CommonController{
	private $model;
	public function __auto(){
		$this->model=new \Common\Model\Type;
	}
	public function index (){
		$data=$this->model->get();
		if(!$data){
			View::error('请先去添加类型',U('add'));
		}
		View::with('data',$data);
		View::make();
	}
	public function add(){
		if(IS_POST){
//			$data=$_POST['tname'];
			$data=Q('post.tname');
//			p($data);exit;
			$oldData=$this->model->where("tname='{$data}'")->find();
			if($oldData){
				View::success("类型已存在");
			}

			if($this->model->store()){
				View::success('添加成功',U('index'));
			}
			View::error($this->model->getError());
		}
		View::make();
	}
//	删除
	public function del(){
		$tid=Q('get.tid',0,'intval');
		$AttributeModel=new \Common\Model\Attribute;
		$AttributeData=$AttributeModel->where("type_tid={$tid}")->find();
		if($AttributeData) View::error('请先删除该类型属性');
		$this->model->where("tid={$tid}")->delete();
		View::success('删除成功');
	}
//	编辑
	public function edit(){
		$tid=Q('get.tid',0,'intval');
		if(IS_POST){
			$this->model->where("tid={$tid}")->update();
			View::success('修改成功',U('index'));
		}
		$data=$this->model->where("tid={$tid}")->find();
		View::with('data',$data);
		View::make();
	}
}
